public class ThreadDemo8 {
    static boolean  isQuit = false;//首先设置为false,后面通过对其改变值来对线程的结束
    public static void main(String[] args) {
        Thread  thread = new Thread(()->{
            while (!isQuit){
                System.out.println("这是一个中断测试!");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("线程结束!");
        });
        thread.start();

        try {
            Thread.sleep(3000);//运行3秒后线程将运行结束!
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("让线程结束!");
        isQuit = true;//改变值控制Thread中的while循环结束,实际也是结束了run方法!
    }
}
